package com.framework.web.bus.service;

import com.framework.web.bus.domain.Cart;
import com.framework.web.bus.repository.CartRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @Title: Service
* @Description: 业务层
* @author fusf
* @date 2016-09-11
* @version V1.0
*
*/
@Service
public class CartService {

@Autowired
private CartRepository  cartRepository;

/**
* 新增或者保存
* @param cart
* @return
*/
public Cart save(Cart cart) {
    return cartRepository.save(cart);
}

/**
* 删除
* @param id
*/
public void  delete(String id) {
    cartRepository.delete(id);
}
/**
* 根据id查询
* @param id
* @return
*/
public Cart findById(String id) {
    return cartRepository.findOne(id);
}

    /**
     * 根据用户和商品Id查询购物车
     * @param userId
     * @param goodsId
     * @return
     */
public Cart findByUserIdAndGoodsId(String userId,String goodsId) {
   List<Cart> cartList = cartRepository.findByUserIdAndGoodsId(userId,goodsId);
    if(cartList!=null && cartList.size()>0)  {
        return cartList.get(0);
    }
    return null;
}

    /**
     * 购物车列表
     * @param userId
     * @return
     */
public List<Cart> findByUserId(String userId) {
 return cartRepository.findByUserId(userId);
}


/**
* 分页查找
* @param cart
* @param pr
* @return
*/
/*public Page<Cart> getPageList(Cart cart, PageRequest pr) {
    Page<Cart> totalList = cartRepository.findAll(CartSpecs.pageQueryCart(cart), pr);
    return totalList;
}*/

}



